package ldkxingzhe.ldkxingzhedemo.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayout;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import ldkxingzhe.ldkxingzhedemo.R;


/**
 * Created by xingzhe on 15/9/23.
 */
public class GridLayoutActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {

    private final String TAG = GridLayoutActivity.class.getSimpleName();

    ViewPager viewPager;
    TextView indexNumber;
    GridLayout gridLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_layout);
        viewPager = (ViewPager)findViewById(R.id.view_pager);
        indexNumber = (TextView)findViewById(R.id.index_number);
        gridLayout = (GridLayout)findViewById(R.id.grid_layout);

        viewPager.setOnPageChangeListener(this);
        PagerAdapter adapter = new ImagePagerAdapter(this);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(0);
        loadButton();

        CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout)findViewById(R.id.collapsing_bar);
        collapsingToolbarLayout.setTitle("林业管理系统");
    }
    /**
     * 加载界面按钮
     * */
    private void loadButton(){
        LayoutInflater inflater = getLayoutInflater();
        for(int i = 0; i < 13; ++i){
            Button button = (Button) inflater.inflate(R.layout.grid_layout_button1, null);
            GridLayout.LayoutParams params = new GridLayout.LayoutParams();
            params.columnSpec = GridLayout.spec(GridLayout.UNDEFINED, GridLayout.RIGHT, 1);;
            params.setGravity(Gravity.FILL_HORIZONTAL);

            button.setOnClickListener(buttonClickListener);
            gridLayout.addView(button,params);
        }
        gridLayout.requestLayout();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        Log.i(TAG,"onPageSelected");
        indexNumber.setText((position + 1) + "/3");
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    // 主按钮的点击事件
    private View.OnClickListener buttonClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    };

    class ImagePagerAdapter extends PagerAdapter{
        private int[] rawImageId = {R.raw.banner1, R.raw.banner2, R.raw.banner3};
        private List<ImageView> imageViewList;

        ImagePagerAdapter(Context context){
            imageViewList = new ArrayList<>();

            DisplayMetrics displayMetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
            int width = displayMetrics.widthPixels;
            int height = (int)(displayMetrics.heightPixels * 0.3);
//            Log.v(TAG,"width is " + width + ", height is " + height);

            for(int i = 0, lenth = rawImageId.length; i < lenth; ++i){
                ImageView tmpImageView = new ImageView(context);
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeResource(context.getResources(), rawImageId[i], options);
                int widthRadio = options.outWidth/width;
                int heightRadio = options.outHeight/height;
//                Log.d(TAG,"widthRadio is " + widthRadio + ", heightRadio is " + heightRadio);
                int resultRadio = 1;
                if(widthRadio > 1 && heightRadio > 1){
                    resultRadio = Math.max(widthRadio, heightRadio);
                }
                options.inJustDecodeBounds = false;
                options.inSampleSize = resultRadio;
//                Log.d(TAG,"inSampleSize is " + resultRadio);
                Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), rawImageId[i]);
                tmpImageView.setScaleType(ImageView.ScaleType.FIT_XY);
                tmpImageView.setImageBitmap(bitmap);
                imageViewList.add(tmpImageView);
            }
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView tmpImageView = imageViewList.get(position);
            container.addView(tmpImageView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            return tmpImageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(imageViewList.get(position));
        }

        @Override
        public int getCount() {
            return rawImageId.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }
}
